package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.enums.UserRelationType;
import com.yuantu.judiciary.model.form.GetQueryInfoForm;
import com.yuantu.judiciary.model.form.SearchRelationForm;
import com.yuantu.judiciary.model.vo.PageVO;
import com.yuantu.judiciary.model.vo.UserRelationVO;
import com.yuantu.judiciary.service.IUserRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户关系模块
 *
 * @author syw
 * @since 2023-06-24
 **/
@RestController
@RequestMapping(value = "/user-relation")
@Api(tags = "用户关系模块")
@RequiredArgsConstructor
public class UserRelationController extends BaseController {

    private final IUserRelationService userRelationService;

    @ApiOperation(value = "添加客户")
    @PostMapping("/add/customer")
    public Response<Boolean> addCustomer(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(userRelationService.add(userId, form, UserRelationType.CUSTOMER));
    }

    @ApiOperation(value = "添加供应商")
    @PostMapping("/add/supplier")
    public Response<Boolean> addSupplier(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(userRelationService.add(userId, form, UserRelationType.SUPPLIER));
    }

    @ApiOperation(value = "取消客户")
    @PostMapping("/cancel/customer")
    public Response<Boolean> cancelCustomer(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(userRelationService.cancel(userId, form, UserRelationType.CUSTOMER));
    }

    @ApiOperation(value = "取消供应商")
    @PostMapping("/cancel/supplier")
    public Response<Boolean> cancelSupplier(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(userRelationService.cancel(userId, form, UserRelationType.SUPPLIER));
    }

    @ApiOperation(value = "获取客户列表")
    @PostMapping("/get/customer/page")
    public Response<PageVO<UserRelationVO>> getCustomerPage(@PageableDefault Pageable pageable, @RequestBody SearchRelationForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(userRelationService.getRelationPage(userId, pageable, UserRelationType.CUSTOMER, form));
    }

    @ApiOperation(value = "获取供应商列表")
    @PostMapping("/get/supplier/page")
    public Response<PageVO<UserRelationVO>> getSupplierPage(@PageableDefault Pageable pageable, @RequestBody SearchRelationForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(userRelationService.getRelationPage(userId, pageable, UserRelationType.SUPPLIER, form));
    }

}
